//
//  WBJPUSHSDKHelper.m
//  Pods-WBJPUSHSDKHelper_Example
//
//  Created by Mr_Lucky on 2018/9/10.
//

#import "WBJPUSHSDKHelper.h"

@implementation WBJPUSHSDKHelper

WBJPUSHSDKHelper *instance = nil;
+ (instancetype)shareHelper {
    if (!instance) {
        instance = [[WBJPUSHSDKHelper alloc]init];
    }
    return instance;
}

- (void)wb_registerJPUSHWithEntity:(JPUSHRegisterEntity *)entity
                            appkey:(NSString *)appKey
                           channel:(NSString *)channel
                   launchingOption:(NSDictionary *)launchingOption
                          delegate:(id<JPUSHRegisterDelegate>)delegate
                       IDFAEnabled:(BOOL)IDFAEnabled {
    
    
    NSString *advertisingId = nil;
    if (IDFAEnabled) {
        advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
    }
    
    if (!entity) {
        entity = [JPUSHRegisterEntity new];
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    }
    
    if (@available(iOS 12.0, *)) {
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound|JPAuthorizationOptionProvidesAppNotificationSettings;
    } else {
        entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    }
    
    [JPUSHService registerForRemoteNotificationConfig:entity
                                             delegate:delegate];
    
    [JPUSHService setupWithOption:launchingOption
                           appKey:appKey
                          channel:channel
                 apsForProduction:WB_JPush_Producation
            advertisingIdentifier:advertisingId];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveMessage:)
                                                 name:kJPFNetworkDidReceiveMessageNotification
                                               object:nil];
    
}

- (void)wb_registrationIDCompletionHandler:(void (^) (int resCode, NSString *registrationID))completionHandler {
    [JPUSHService registrationIDCompletionHandler:completionHandler];
}

- (void)clearNotificationBadge:(UIApplication *)application {
    application.applicationIconBadgeNumber = 0;
    [application cancelAllLocalNotifications];
}

// MARK:通知
- (void)didReceiveMessage:(NSNotification *)noti {
    if (_delegate && [_delegate respondsToSelector:@selector(jpushDidReceiveMessage:)]) {
        [_delegate jpushDidReceiveMessage:noti];
    }
}

@end
